~ chicken-core (chicken-5) /manual/Module (chicken read-syntax)
Trap1[[tags: manual]]2[[toc:]]34== Module (chicken read-syntax)56This module provides procedures which can be used to extend the reader7with custom read syntax.89=== define-reader-ctor1011<procedure>(define-reader-ctor SYMBOL PROC)</procedure>1213Define new read-time constructor for {{#,}} read syntax. For further information, see14the documentation for [[http://srfi.schemers.org/srfi-10/srfi-10.html|SRFI-10]].151617=== set-read-syntax!1819<procedure>(set-read-syntax! CHAR-OR-SYMBOL PROC)</procedure>2021When the reader encounters the non-whitespace character {{CHAR}} while reading22an expression from a given port, then the procedure {{PROC}} will be called with23that port as its argument. The procedure should return a value that will be returned24to the reader:2526<enscript highlight=scheme>27 ; A simple RGB color syntax:2829 (set-read-syntax! #\%30 (lambda (port)31 (apply vector32 (map (cut string->number <> 16)33 (string-chop (read-string 6 port) 2) ) ) ) )3435 (with-input-from-string "(1 2 %f0f0f0 3)" read)36 ; ==> (1 2 #(240 240 240) 3)37</enscript>3839If {{CHAR-OR-SYMBOL}} is a symbol, then a so-called ''read-mark'' handler is defined.40In that case the handler procedure will be called when a character-sequence of the41form {{#!SYMBOL}} is encountered.4243You can undo special handling of read-syntax by passing {{#f}} as the second argument44(if the syntax was previously defined via {{set-read-syntax!}}).4546As a special case, your handler can return zero values, via {{(values)}}. This causes47the reader to completely ignore whatever input you've read, rather than returning some48possibly unspecified value. This can be useful in macro context, reading comments,49conditional compilation, and so forth. Available in CHICKEN 4.6.6 and later.5051Note that all of CHICKEN's special non-standard read-syntax is handled directly by the reader.52To disable built-in read-syntax, define a handler that triggers an error (for example).535455=== set-sharp-read-syntax!5657<procedure>(set-sharp-read-syntax! CHAR-OR-SYMBOL PROC)</procedure>5859Similar to {{set-read-syntax!}}, but allows defining new {{#<CHAR> ...}} reader syntax.60If the first argument is a symbol, then this procedure is equivalent to {{set-read-syntax!}}.6162{{PROC}} may be {{#f}} to disable previously defined "sharp" read syntax.636465=== set-parameterized-read-syntax!6667<procedure>(set-parameterized-read-syntax! CHAR-OR-SYMBOL PROC)</procedure>6869Similar to {{set-sharp-read-syntax!}}, but intended for defining reader syntax of the70form {{#<NUMBER><CHAR> ...}}. The handler procedure {{PROC}} will be called with two71arguments: the input port and the number preceding72the dispatching character.73If the first argument is a symbol, then this procedure is equivalent to {{set-read-syntax!}}.7475{{PROC}} may be {{#f}} to disable previously defined parameterized read syntax.767778=== copy-read-table7980<procedure>(copy-read-table READ-TABLE)</procedure>8182Returns a copy of the given read-table. You can access the currently83active read-table with {{(current-read-table)}}. This procedure can84be useful to restore an old read-table after temporarily introducing85new read syntax.868788=== current-read-table8990<parameter>(current-read-table)</parameter>9192A read-table object that holds read-procedures for special non-standard93read-syntax (see {{set-read-syntax!}} for more information).949596---97Previous: [[Module (chicken random)]]9899Next: [[Module (chicken repl)]]